﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class FeedBackCustomerController : BaseController<feedback>
    {
        //
        // GET: /FeedBackCustomer/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<feedback> bll
        {
            get
            {
                return new FeedBackCusBll();
            }
        }

        UserBll userbll = new UserBll();

        /// <summary>
        /// 定义类  前台显示
        /// </summary>
        public class ListNoticeModel  //feedbackId  feedbackValue  feedbackTime wechatCode userName wechatName
        {
            public long feedbackId;
            public string feedbackValue;
            public string feedbackTime;
            public string wechatCode;
            public int userCode;
            public string userName;
            public string wechatName;
            public string userPhone;
            public int? IsReply;

        }
        /// <summary>
        /// 加载数据列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="model"></param>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public ActionResult GetNotice(int page, int rows, feedback model, string date1, string date2)
        {
            int count;
            //var listAfter = new List<ListNoticeModel>();
            var listNoticeModel = new List<ListNoticeModel>();
            //1. 查询出意见表表里的所有的信息                           
            Expression<Func<feedback, bool>> condition1 = x => true;
            if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
            {
                // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
                condition1 = x => true;
            }
            else
            {
                // 1.先筛选出这个时间段的 满足要求的信息 
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition1 = x => x.feedbackTime >= datetime1 && x.feedbackTime <= datetime2;
            }
            //model, x => true, false, x => x.feedbackId, rows, page, out count
            var listNotice = bll.Search().Where(condition1).Select(x => new { x.feedbackId, x.feedbackValue, x.IsReply, x.feedbackTime, x.wechatCode, }).ToList();

            foreach (var oneCode in listNotice)
            {
                //根据订单详情表里的商品编号  进行查询用户表的相关信息    
                var list2 = userbll.Search(x => x.wechatCode == oneCode.wechatCode).Select(x => new { x.wechatCode, x.userName, x.wechatName, x.userPhone, x.userCode }).ToList();
                if (list2.Count != 0)
                {
                    ListNoticeModel salePro = new ListNoticeModel();
                    salePro.feedbackId = oneCode.feedbackId;
                    salePro.wechatCode = oneCode.wechatCode;
                    salePro.IsReply = oneCode.IsReply;           // == 1 ? "已回复" : "点击回复";
                    salePro.wechatName = list2.FirstOrDefault().wechatName;
                    salePro.userName = list2.FirstOrDefault().userName;
                    salePro.userPhone = list2.FirstOrDefault().userPhone;
                    salePro.feedbackValue = oneCode.feedbackValue;
                    salePro.userCode = list2.FirstOrDefault().userCode;
                    salePro.feedbackTime = oneCode.feedbackTime.ToString("yyyy-MM-dd  HH:mm:ss");
                    listNoticeModel.Add(salePro);
                }
            }

            var listNotice2 = bll.Search(x=>x.IsRead==0).Where(condition1).Select(x => new { x.feedbackId, x.feedbackValue, x.IsReply, x.feedbackTime, x.wechatCode, }).ToList();

            //修改   未读已读状态

            foreach (var item in listNotice2)
            {
                feedback model1 = new feedback();
                model1.feedbackId = item.feedbackId;
                model1.wechatCode = item.wechatCode;

                model1.feedbackValue = item.feedbackValue;
                model1.feedbackTime = item.feedbackTime;
                model1.IsReply = item.IsReply;
                model1.IsRead = 1;//已读
                if (bll.Modify(model1) > 0)
                {

                }
            }
            var listAfter = listNoticeModel.OrderByDescending(x => x.feedbackTime).ToPagedList(page, rows).ToList();
            return Json(new { rows = listAfter, total = listNoticeModel.Count () });
        }


        public ActionResult ModifyReply(int userCode)
        {
            string msg = "回复失败";
            string state = "false";
            feedback model = new feedback();
            var list = userbll.Search(x => x.userCode == userCode).ToList();
            string code = list.FirstOrDefault().wechatCode;
            var list2 = bll.Search(x => x.wechatCode == code).ToList();
            model.feedbackId = list2.FirstOrDefault().feedbackId;
            model.feedbackTime = list2.FirstOrDefault().feedbackTime;
            model.feedbackValue = list2.FirstOrDefault().feedbackValue;
            model.wechatCode = list2.FirstOrDefault().wechatCode;
            model.IsReply = 1;
            if (bll.Modify(model) > 0)
            {
                msg = "回复成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });

        }


    }
}
